import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class PointerMoveIndicatorRoute extends StatefulWidget {
  const PointerMoveIndicatorRoute({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _PointerMoveIndicatorRouteState();
}

class _PointerMoveIndicatorRouteState extends State<PointerMoveIndicatorRoute> {
  final logger = Logger(printer: LogfmtPrinter());

  PointerEvent? _event;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('原始指针事件处理'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Listener(
              child: Container(
                alignment: Alignment.center,
                color: Colors.blue,
                width: 300.0,
                height: 150.0,
                child: Text(
                  '${_event?.localPosition ?? ''}',
                  style: const TextStyle(color: Colors.white),
                ),
              ),
              //手指按下回调
              onPointerDown: (e) => setState(() => _event = e),
              //手指移动回调
              onPointerMove: (e) => setState(() => _event = e),
              //手指抬起回调
              onPointerUp: (e) => setState(() => _event = e),
            ),
            const SizedBox(height: 10),
            Listener(
              child: AbsorbPointer(
                child: Listener(
                  child: Container(
                    color: Colors.red,
                    width: 200.0,
                    height: 100.0,
                  ),
                  onPointerUp: (e) => logger.i("onPointerUp in"),
                ),
              ),
              onPointerDown: (event) => logger.i("up"),
            ),
          ],
        ),
      ),
    );
  }
}
